package com.example.juyixia;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.adapter.MessageAdapter;
import com.example.adapter.MessageAdapter.Info;
import com.example.adapter.MyAdapter;
import com.example.database.DbManager;
import com.exanple.data.ChatMsgEntity;
import com.exanple.data.Messageinfo;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class Chat_Fragment extends Fragment {
	private View view;
	private Myinfo myinfo;
	private ListView messagelist;
	private BroadcastReceiverFromService broadcastReceiverFromService;
	private DbManager mgr;
	private List<Messageinfo> list = new ArrayList<Messageinfo>();
    private TextView delete;
    private MessageAdapter mAdapter;
    private int position;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		myinfo = (Myinfo) getActivity().getApplication();		
		mgr = new DbManager(this.getActivity());
		broadcastReceiverFromService = new BroadcastReceiverFromService();
		IntentFilter filter = new IntentFilter();
		filter.addAction("fragment.action.service");
		getActivity().registerReceiver(broadcastReceiverFromService, filter);
		view = inflater.inflate(R.layout.fragment_01, container, false);
		messagelist = (ListView) view.findViewById(R.id.messagelist);
		list = Messageinfo.messlist;
        
		 mAdapter = new MessageAdapter(getActivity(), list);
		// 设置适配器
		messagelist.setAdapter(mAdapter);
		 messagelist.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
					// TODO Auto-generated method stub	
					
					Messageinfo messinfo=(Messageinfo) messagelist.getItemAtPosition(position);
					Intent intent=new Intent(getActivity(), Activity_chatActivity.class);
					 Bundle bundle=new Bundle();
		             bundle.putString("name", messinfo.getSendname());
		             bundle.putString("ip",messinfo.getIp());
		             bundle.putString("publickey",messinfo.getPublickey());
	                                                               
		             intent.putExtras(bundle);
	                //利用上下文开启跳转
	                startActivity(intent);  
		                        
				}
			});
		 messagelist.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> partent, View thisview, final int arg2, long arg3) {
				// TODO Auto-generated method stub
				position=arg2;
				
				delete=(TextView) thisview.findViewById(R.id.tv_delete);
				delete.setVisibility(view.VISIBLE);
				delete.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
					delete.setVisibility(View.GONE);
					list.remove(arg2);
					mAdapter.notifyDataSetChanged();
					}
				});
				return true;
			}
		    
		 
		 });

         
		return view;
	}
     
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

	}

	public class BroadcastReceiverFromService extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			list = Messageinfo.messlist;
			// 适配器，刚刚重写的！
			MessageAdapter mAdapter = new MessageAdapter(getActivity(), list);
			// 设置适配器
			messagelist.setAdapter(mAdapter);
		}

	}
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		list = Messageinfo.messlist;
		// 适配器，刚刚重写的！
		MessageAdapter mAdapter = new MessageAdapter(getActivity(), list);
		// 设置适配器
		messagelist.setAdapter(mAdapter);
	}
	

}
